package com.gxuwz.stats.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.stats.config.CurrentLoginUserUtil;
import com.gxuwz.stats.entity.TotalWork;
import com.gxuwz.stats.service.TotalWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/userTotalWork")
public class TotalWorkUserController {
    @Autowired
    private TotalWorkService totalWorkService;

    @GetMapping("/totalWorkUserList")
    public ResponseEntity<Map<String, Object>> totalWorkList(
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            // 使用工具类获取当前用户ID
            Integer currentUserId = CurrentLoginUserUtil.getCurrentUserId();
//            System.out.println("工具类获取到 的当前用户Id="+currentUserId);
            if (currentUserId == null) {
                Map<String, Object> response = new HashMap<>();
                response.put("code", 401);
                response.put("message", "用户未登录");
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
            }

            // 创建 MyBatis Plus 的 Page 对象进行分页查询
            IPage<TotalWork> page = new Page<>(currentPage, pageSize);
            QueryWrapper<TotalWork> queryWrapper = new QueryWrapper<>();

            // 添加当前登录用户的过滤条件
            queryWrapper.eq("user_id", currentUserId); // 根据 user_id 进行过滤


            // 调用 service 层的分页查询方法
            IPage<TotalWork> resultPage = totalWorkService.getTotalWorkListWithUserInfo(page, queryWrapper);
//            System.out.println("用户总工作量的查询结果="+resultPage);

//            // 构造返回结果
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "查询成功");
            response.put("data", resultPage.getRecords()); // 当前页数据
            response.put("total", resultPage.getTotal()); // 总记录数
            System.out.println("总记录数="+resultPage.getTotal());
            response.put("currentPage", resultPage.getCurrent()); // 当前页码
            response.put("pageSize", resultPage.getSize()); // 每页大小
            response.put("totalPages", resultPage.getPages()); // 总页数

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }
//    @GetMapping("/totalWorkUserList")
//    public ResponseEntity<Map<String, Object>> totalWorkList(
//            @RequestParam(defaultValue = "1") int currentPage,
//            @RequestParam(defaultValue = "10") int pageSize) {
//        try {
//            // 创建 MyBatis Plus 的 Page 对象进行分页查询
//            IPage<TotalWork> page = new Page<>(currentPage, pageSize);
//            QueryWrapper<TotalWork> queryWrapper = new QueryWrapper<>();
//
//            // 调用 service 层的分页查询方法（带用户信息）
//            IPage<TotalWork> resultPage = totalWorkService.getTotalWorkListWithUserInfo(page, queryWrapper);
//
//            // 构造返回结果
//            Map<String, Object> response = new HashMap<>();
//            response.put("code", 200);
//            response.put("message", "查询成功");
//            response.put("data", resultPage.getRecords()); // 当前页数据（包含用户名）
//            response.put("total", resultPage.getTotal()); // 总记录数
//            response.put("currentPage", resultPage.getCurrent()); // 当前页码
//            response.put("pageSize", resultPage.getSize()); // 每页大小
//            response.put("totalPages", resultPage.getPages()); // 总页数
//
//            return ResponseEntity.ok(response);
//        } catch (Exception e) {
//            e.printStackTrace();
//            Map<String, Object> errorResponse = new HashMap<>();
//            errorResponse.put("code", 500);
//            errorResponse.put("message", "查询失败: " + e.getMessage());
//            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
//        }
//    }
}
